package com.lins.store.controller;

import com.lins.store.bo.FileService;
import com.lins.store.bo.UserService;
import com.lins.websocket.WsServer;
import com.lins.store.dto.UserDTO;
import com.lins.store.entity.UserEntity;
import com.lins.common.util.JsonResult;
import com.lins.common.util.LinsUtil;
import com.lins.common.util.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
//@CrossOrigin(origins = "*") //跨域相关
@RequestMapping("users")
@Api(value = "用户接口",tags = {"用户接口"}) //接口名称和标签名
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    @Autowired
    private WsServer wsServer;

    /*@GetMapping({"", "/"})
    @ApiOperation("获取用户列表")
    public JsonResult<List<UserEntity>> getUserList() {
        List<UserEntity> userEntityList = userService.getUserList();
        return new JsonResult<>(OK, userEntityList);
    }*/

    @GetMapping({"", "/"})
    @ApiOperation("获取用户列表")
    public JsonResult<List<UserDTO>> getUserList() {
        List<UserDTO> result = new ArrayList<>();
        for (UserEntity entity:userService.getUserList()){
            UserDTO dto = new UserDTO();
            BeanUtils.copyProperties(entity,dto);
            result.add(dto);
        }
        return new JsonResult<>(OK, result);
    }

    @PostMapping("deleteUserByUid/{uid}")
    @ApiOperation("根据uid删除用户")
    public JsonResult<String> delete(@PathVariable("uid") String uid) {
        Integer delUser = userService.delUserByUid(uid);
        Integer delFile = fileService.delFileByUid(uid);

        HttpSession session = SessionUtils.getInstance().getSession(Integer.parseInt(uid));

        //通过websocket通知用户被强制离线
        wsServer.sendMessage(session.getId(),"你的账号被禁用！请联系管理员");

        //删除用户账号时，也失效他的session，防止再次请求
//        HttpSession session = (HttpSession) sessionMap.get(uid); //普通Map不行
        //通过实例来取出session，并失效它
        session.invalidate();

        return new JsonResult<>(OK, LinsUtil.print("删除用户信息{}！连带删除用户文件{}条",delUser > 0 ? "成功" : "失败",delFile));
    }

    @GetMapping("sendMsg/{uid}/{msg}")
    @ApiOperation("给x用户发送消息x")
    public void sendMessage(@PathVariable("uid") String uid,@PathVariable("msg") String msg){
        wsServer.sendMessage(SessionUtils.getInstance().getSession(Integer.parseInt(uid)).getId(),msg);
    }

    @GetMapping("get_by_uid")
    @ApiOperation("根据uid获取用户信息")
    public JsonResult<UserEntity> getByUid(HttpSession session) {
        return new JsonResult<>(OK, userService.getByUid(getUidFromSession(session)));
    }




    /**
     * fileController类已经被aop给管理
     *  本接口用于测试自定义的AOP注解(MyAnnotation)的使用:
     *  执行本方法之前，会先执行注解，把testName字符串值赋值给注解内的接口方法(抽象方法)。
     *  AOP切面执行增强方法，point接入点指向目标方法(也就是本方法)，执行point.proceed()才会真正调用本方法。
     *  实现了在本方法前后，加入一些执行逻辑，增强本方法。
     *
     *
     */
//    @GetMapping("aopAnnotationTest/{name}")
//    @MyAnnotation(methodName = "testName")
//    @ApiOperation("AOP注解测试")
//    public void aopAnnotationTest(@PathVariable("name") String name){
//        log.info("hello {}!",name);
//    }
//
//    @GetMapping("aopTest2/{name}")
//    @TimedTaskLog(name = "testName",time = "2022-11-4")
//    @ApiOperation("AOP注解测试")
//    public void aopAnnotationTest2(@PathVariable("name") String name){
//        log.info("hello {}!",name);
//    }

}